package com.zhonghui.common.eshopService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDomain.StoreCombination;
import com.zhonghui.common.eshopMapper.StoreCombinationMapper;
import com.zhonghui.common.eshopService.StoreCombinationService;
import com.zhonghui.common.eshopVo.CombinationQueryVo;
import com.zhonghui.common.eshopVo.StoreCombinationQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(
        propagation = Propagation.REQUIRED,
        rollbackFor = {Exception.class}
)
public class StoreCombinationServiceImpl extends BaseServiceImpl<StoreCombinationMapper, StoreCombination> implements StoreCombinationService {
    @Autowired
    private StoreCombinationMapper storeCombinationMapper;

    public CombinationQueryVo getList(int page, int limit) {
        CombinationQueryVo combinationQueryVo = new CombinationQueryVo();
        Date nowTime = new Date();
        Page<StoreCombination> pageModel = new Page((long)page, (long)limit);
        LambdaQueryWrapper<StoreCombination> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(StoreCombination::getIsShow, 1).le(StoreCombination::getStartTime, nowTime)
                .ge(StoreCombination::getStopTime, nowTime).orderByAsc(StoreCombination::getSort);
        IPage<StoreCombination> yxStoreCombinationIPage = this.storeCombinationMapper.selectPage(pageModel, wrapper);
        List<StoreCombinationQueryVo> collect = (List)yxStoreCombinationIPage.getRecords().stream().map((i) -> {
            StoreCombinationQueryVo storeCombinationQueryVo = new StoreCombinationQueryVo();
            BeanUtils.copyProperties(i, storeCombinationQueryVo);
            return storeCombinationQueryVo;
        }).collect( Collectors.toList());
        combinationQueryVo.setStoreCombinationQueryVos(collect);
        combinationQueryVo.setLastPage(yxStoreCombinationIPage.getPages());
        return combinationQueryVo;
    }
}
